/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TUser;
import com.gmail.blackdog1987.carcare.network.request.builder.LoginRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.LoginResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.LoginResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;

/**
 * @author wdy
 *
 */
public class LoginActivity extends BaseActivity implements OnResponseListener,
		OnClickListener {
	private Context mContext;
	private static final String TAG = LoginActivity.class.getSimpleName();
	private EditText mId;
	private EditText mPwd;

	private LoginRequestBuilder mReq = new LoginRequestBuilder();
	private LoginResponseHandler mHandler = new LoginResponseHandler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		mContext = this;
		initViews();
		reg();
	}

	@Override
	public void initViews() {
		super.initViews();
		mId = (EditText) findViewById(R.id.login_id);
		mPwd = (EditText) findViewById(R.id.login_pwd);
	}

	@Override
	public void reg() {
		super.reg();
		findViewById(R.id.login_ok).setOnClickListener(this);
		findViewById(R.id.login_reg).setOnClickListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		mHandler.setOnResponseListener(this);
	}

	@Override
	public void initData() {
		super.initData();
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		TUser TUser = ((LoginResponse)classz.getResponse()).user;
		new UserDao().saveUser(TUser);
		setResult(Activity.RESULT_OK);
		Toast.makeText(mContext, "登录成功", Toast.LENGTH_LONG).show();
		finish();
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "用户名或密码错误", Toast.LENGTH_LONG).show();
	}

	private boolean validInput() {
		if (mId.getText().toString().length() == 0) {
			Toast.makeText(mContext, "请输入账号", Toast.LENGTH_LONG).show();
			return false;
		}
		if (mPwd.getText().toString().length() == 0) {
			Toast.makeText(mContext, "密码错误", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_ok:
			if (validInput()) {
				mReq.setUsername(mId.getText().toString()).setPwd(mPwd.getText().toString()).postJsonRequest(mContext, mHandler);
			}
			break;
		case R.id.login_reg:
			startActivity(new Intent(LoginActivity.this, RegActivity.class));
			break;
		case R.id.back:
			finish();
			break;
		}
	}

}
